# This Python file uses the following encoding: utf-8

from django.db import models
from alumnes.models import Curs
from django.core.exceptions import ValidationError, NON_FIELD_ERRORS

class TipusDAssignatura(models.Model):
    AMBIT_CHOICES = (
	    ('G', 'Grup'),
        ('C', 'Curs'),
        ('N', 'Nivell'),
        ('I', 'Institut'),
	    ('X', 'No Admet alumnes (Ex: G)'),
	)
    id_tipus_assignatura = models.AutoField(primary_key=True)
    tipus_assignatura = models.CharField(max_length=45, unique=True)
    ambit_on_prendre_alumnes = models.CharField(max_length=45,choices=AMBIT_CHOICES, default='G', blank=False, null=False)
    class Meta:
        db_table = u'tipus_d_assignatura'
        verbose_name = u'Tipus d\'assignatura'
        verbose_name_plural = u'Tipus d\'assignatura'

    def __unicode__(self):
        return self.tipus_assignatura

    def delete(self):
        if self.assignatura_set.count() > 0:
            errors = {}
            errors[NON_FIELD_ERRORS] = [u'''Aquest tipus d'assignatura es fa servir, no el pots esborrar''']
            raise ValidationError(errors)
        self.save()

class Assignatura(models.Model):
    id_assignatura = models.AutoField(primary_key=True)
    curs = models.ForeignKey(Curs, null=True, db_column='id_curs', blank=True)
    tipus_assignatura = models.ForeignKey(TipusDAssignatura, null=True, db_column='id_tipus_assignatura', blank=True)
    codi_assignatura = models.CharField(max_length=45)
    nom_assignatura = models.CharField(max_length=250, blank=True)
    enllac_kronowin= models.CharField(max_length=45, blank=True)
    class Meta:
        db_table = u'assignatura'
        verbose_name = u'Assignatura'
        verbose_name_plural = u'Assignatures'
    def __unicode__(self):        
        curs = u'({0})'.format(self.curs.nom_curs) if self.curs else ''
        return u'{0}{1}'.format(self.codi_assignatura,curs )
    
    def clean(self):
        rules.Assignatura_clean(self)
        
    def  getLongName(self):
        return self.nom_assignatura.title() if self.nom_assignatura and self.codi_assignatura != self.nom_assignatura else self.codi_assignatura

class UnitatFormativa(models.Model):
    
    #
    # TODO -> Donada una assignatura i un grup !!!!!!!!!!!!!!!!!!!!!!!
    #
    
    id_unitat_formativa = models.AutoField(primary_key=True)
    assignatura = models.ForeignKey(Assignatura, db_column='id_assignatura')
    data_inici_unitat_formativa = models.DateField(null=True, blank=True)
    data_fi_unitat_formativa = models.DateField(null=True, blank=True)
    numero_unitat_formativa = models.IntegerField()
    nom_unitat_formativa = models.CharField(max_length=250)
    class Meta:
        db_table = u'unitats_formatives'
        verbose_name = u'Unitat Formativa'
        verbose_name_plural = u'Unitats Formatives'
        unique_together = (("assignatura", "numero_unitat_formativa"))



# ----------------------------- B U S I N E S S       R U L E S ------------------------------------ #
from assignatures import rules
rules.connecta()